package com.deer.wms.base.system.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author 申紫微
 * @version 1.0
 * @description 切面类，监控请求响应时长
 * @data 2020-10-16 09:04:45
 */

@Component
@Slf4j
@Aspect
public class ControllerWatcher {


    /**
     * @description 切面位置
     * 表达式第一个* 代表任意的返回值
     * com.aitplus.lijinji.controller aop所横切的包名，包后面..表示当前包及其子包
     * 第二个*代表类名， 代表所有类.*(..) 表示任何方法，括号代表参数 .. 表示任意参数
     * @date 9:15 2020/7/16
     * @author 申紫微
     **/
    @Pointcut("execution(public * com.deer.wms.base.system.web..*.*(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before() {
        log.info("ControllerWatcher pointCut before");
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("------ watch start ------");
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        log.info("request {}", joinPoint.getTarget().getClass().getMethod
                (methodSignature.getMethod().getName(), methodSignature.getMethod().getParameterTypes()));
        long start = System.nanoTime();
        Object result = joinPoint.proceed();
        long end = System.nanoTime();
        log.info("------ watch end, cost {} ms", (end - start) / (1000 * 1000));
        return result;
    }

    @After("pointCut()")
    public void after(){
        log.info("ActionWatcher pointCut after");
    }
}
